package com.changgou.seckill;

import entity.IdWorker;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import tk.mybatis.spring.annotation.MapperScan;

import java.net.UnknownHostException;

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = "com.changgou.seckill.dao")

@EnableScheduling  //任务调度,定时器的启动注解

@EnableAsync  //开启多线程的注解
public class SeckillApplication {

    public static void main(String[] args) {

        SpringApplication.run(SeckillApplication.class,args);
    }


    //用来设置redis
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)  {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        //设置key存储的序列化方式为 字符串
        template.setKeySerializer(new StringRedisSerializer());  //设置redis中的key为string类型

        //template.setHashKeySerializer(new StringRedisSerializer());
        //template.setHashValueSerializer(new StringRedisSerializer());  设置redis中的value为string类型

        return template;
    }

    @Autowired
    private Environment env;

    @Bean
    public IdWorker idWorker(){

        return  new IdWorker(0,4);
    }


    //创建秒杀交换机
    @Bean
    public DirectExchange createSeckillDirect() {
        return new DirectExchange(env.getProperty("mq.pay.exchange.seckillorder"));
    }

    //创建秒杀队列
    @Bean
    public Queue createSeckillqueue() {
        return new Queue(env.getProperty("mq.pay.queue.seckillorder"));
    }

    //创建 秒杀的绑定
    @Bean
    public Binding createSeckillBinding() {
        return BindingBuilder.bind(createSeckillqueue()).to(createSeckillDirect()).with(env.getProperty("mq.pay.routing.seckillkey"));
    }
}
